package com.slf.engine.threads;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBossService;
import com.slf.engine.services.IHpBossService;
import com.slf.engine.services.impl.BossServiceImpl;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.HpbossServiceImpl;
import com.slf.engine.utils.Factory;


public class GetRequestThread extends Thread {

	private static final org.apache.log4j.Logger log = Logger.getLogger(GetRequestThread.class);
	
	
	public GetRequestThread()
	{
		log.log(Priority.INFO, "GET_REQUEST_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		IBossService bossService = new BossServiceImpl();
		//IHpBossService bossService = new HpbossServiceImpl();
		while(true)
		{
			if(SysContext.IS_START)
			{
				Order order = bossService.getOneOrder();
				if(order != null)
				{
					IBaseDao baseDao = (IBaseDao) Factory.getInstantBiz("baseDao");
						try {
							baseDao.add(order, "addOrder");
							log.log(Priority.INFO, "交易队列添加订单成功order_id:" + order.getOrderId() + " account:" + order.getAccount() + " money:" + order.getMoney());
							//bossService.doPost(order);
							SysContext.ORDER_LIST.add(order);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							try {
								//log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
								Thread.sleep(3000);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								
							}
						}
	//				}
				}else{
					try {
						//log.log(Priority.INFO, "没有获取到交易记录---3秒后重新获取");
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						
					}
				}
			}else{
				try {
					log.log(Priority.INFO, "等待上游服务连接,3秒后重试获取交易线程");
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					
				}
			}
		}
	}
}
